package com.yuye.controller;

import com.yuye.entity.SysInfoEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Properties;

/**
 *  系统信息接口
 *
 *  @author: yuye
 *  @Date: 2017/5/8 21:43
 */
@RestController
@RequestMapping("/sys")
public class SysInfoController {

    @RequestMapping("/getSysInfo")
    public SysInfoEntity getSysInfo(Model model){
        Properties props=System.getProperties(); //获得系统属性集
        Runtime r = Runtime.getRuntime();
        String osName = props.getProperty("os.name"); //操作系统名称
        String osArch = props.getProperty("os.arch"); //操作系统构架
        String osVersion = props.getProperty("os.version"); //操作系统版本
        String java = props.getProperty("java.version");//JDK 版本
        Long memory = r.totalMemory()/1024/1024;//总内存
        Long freeMemory = r.freeMemory()/1024/1024;//剩余内存
        Long useMemory = memory - freeMemory;
        int cpu = r.availableProcessors();//可用CPU线程数
        SysInfoEntity sysInfo = new SysInfoEntity();
        sysInfo.setOsName(osName);
        sysInfo.setOsArch(osArch);
        sysInfo.setOsVersion(osVersion);
        sysInfo.setJdkVersion(java);
        sysInfo.setMemory(memory);
        sysInfo.setFreeMemory(freeMemory);
        sysInfo.setUseMemory(useMemory);
        sysInfo.setCpu(cpu);
        return sysInfo;
    }
}
